<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>drawCircle</title>
    <style>
        #game {
            background-color: #333;
        }
    </style>
</head>
<body>
<header>
    <h1>Drawing in Canvas</h1>
</header>
<canvas id="game" width="768" height="400">
    滚
</canvas>
<script src="../bower_components/jq/dist/jquery.min.js"></script>
<script>
    const untangleGame = {
        circles: [],
        thinLineThickness: 1,
        lines: []
    }

    $(function () {
        let canvas = document.getElementById('game')
        let ctx = canvas.getContext('2d')

        let width = canvas.width
        let height = canvas.height

        let circleRadius = 10
        let circleCount = 5
        for (let i = 0; i < circleCount; i++) {
            let x = Math.random() * width
            let y = Math.random() * height
            drawCircle(canvas, randomColor(), x, y, circleRadius, 0, Math.PI * 2, true)
            untangleGame.circles.push(new Circle(x, y, circleRadius))
        }

        connectCircle(canvas)

        $('#game').mousedown(function (e) {
            let canvasPosition = $(this).offset()
            let mouseX = (e.pageX - canvasPosition.left) || 0
            let mouseY = (e.pageY - canvasPosition.top) || 0

            for (let i = 0; i < untangleGame.circles.length; i++) {
                let circleX = untangleGame.circles[i].x
                let circleY = untangleGame.circles[i].y
                let radius = untangleGame.circles[i].radius
                if (Math.pow(mouseX - circleX, 2) + Math.pow(mouseY - circleY, 2) < Math.pow(radius, 2)) {
                    untangleGame.targetCircle = i
                    break
                }
            }
        })

        $('#game').mousemove(function (e) {
            if (untangleGame.targetCircle !== undefined) {
                let canvasPosition = $(this).offset()
                let mouseX = (e.pageX - canvasPosition.left) || 0
                let mouseY = (e.pageY - canvasPosition.top) || 0
                let radius = untangleGame.circles[untangleGame.targetCircle].radius
                untangleGame.circles[untangleGame.targetCircle] = new Circle(mouseX, mouseY, radius)
            }
            connectCircle(canvas)
        })

        $('#game').mouseup(function (e) {
            untangleGame.targetCircle = undefined
        })

        setInterval(gameLoop, 1000/60)
    })

    class Circle {
        constructor(x, y, radius) {
            this.x = x
            this.y = y
            this.radius = radius
        }
    }

    class Line {
        constructor(startPiont, endPoint, thickness) {
            this.startPiont = startPiont
            this.endPoint = endPoint
            this.thickness = thickness
        }
    }

    function connectCircle(canvas) {
        untangleGame.lines.length = 0
        for (let i = 0; i < untangleGame.circles.length; i++) {
            let startPoint = untangleGame.circles[i]
            for (let j = 0; j < i; j++) {
                let endPoint = untangleGame.circles[j]
                untangleGame.lines.push(new Line(startPoint, endPoint, untangleGame.thinLineThickness))
            }
        }
    }

    function drawLine(canvas, x1, y1, x2, y2, lineWidth, strokeStyle) {
        let ctx = canvas.getContext('2d')
        ctx.beginPath()
        ctx.moveTo(x1, y1)
        ctx.lineTo(x2, y2)
        ctx.lineWidth = lineWidth
        ctx.strokeStyle = strokeStyle
        ctx.stroke()
    }

    function drawCircle(canvas, fillStyle, x, y, radius, beginAngle, endAngle, acticlockwise) {
        let ctx = canvas.getContext('2d')
        ctx.fillStyle = fillStyle
        ctx.beginPath()
        ctx.arc(x, y, radius, beginAngle, endAngle, acticlockwise)
        ctx.closePath()
        ctx.fill()
    }

    function gameLoop() {
        let canvas = document.getElementById('game')
        let ctx = canvas.getContext('2d')

        clearCanvas(canvas)
        for (let i = 0;i<untangleGame.lines.length;i++){
            let line = untangleGame.lines[i]
            let startPoint = line.startPiont
            let endPoint = line.endPoint
            let thickness = line.thickness
            drawLine(canvas,startPoint.x,startPoint.y,endPoint.x,endPoint.y,thickness,randomColor())
        }

        for (let i = 0;i<untangleGame.circles.length;i++){
            let circle = untangleGame.circles[i]
            drawCircle(canvas,randomColor(),circle.x,circle.y,circle.radius,0, Math.PI * 2, true)
        }
    }

    function randomColor() {
        return `rgba(${Math.random() * 255},${Math.random() * 255},${Math.random() * 255},${Math.random()})`
    }

    function clearCanvas(canvas) {
        let ctx = canvas.getContext('2d')
        ctx.clearRect(0, 0, canvas.width, canvas.height)
    }

</script>
</body>
</html>
